// pages/go_wu_che/go_wu_che.js
import {request} from "../../utils/http.js"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dingdan:[],
    allChecked: true, // 全选状态
    totalPrice: "0.00", // 总价
    goods:[
      {
        url:"http://122.51.84.231:9001/dianshang/鸡蛋.png",
        text1:"语农 散装土鸡蛋 360枚 40斤",
        text2:"40斤 正负25g",
        money:28.88,
        num:1,
        checked: true,
      },
      {
        url:"http://122.51.84.231:9001/dianshang/eggs.png",
        text1:"语农 散装笨鸡蛋 360枚 40斤",
        text2:"40斤 正负20g",
        money:16.9,
        num:1,
        checked: true,
      },
      {
        url:"http://122.51.84.231:9001/dianshang/鸡蛋.png",
        text1:"语农 散装土鸡蛋 360枚 40斤",
        text2:"40斤 正负25g",
        money: 28.8,
        num: 1,
        checked: true,
      },
      {
        url:"http://122.51.84.231:9001/dianshang/鸡蛋.png",
        text1:"语农 散装土地瓜 30枚 80斤",
        text2:"30斤 正负60g",
        money: 88.8,
        num: 1,
        checked: true,
      },
      {
        url:"http://122.51.84.231:9001/dianshang/鸡蛋.png",
        text1:"语农 散装土地瓜 30枚 80斤",
        text2:"30斤 正负60g",
        money: 88.8,
        num: 1,
        checked: true,
      },
      {
        url:"http://122.51.84.231:9001/dianshang/鸡蛋.png",
        text1:"语农 散装土地瓜 30枚 80斤",
        text2:"30斤 正负60g",
        money: 88.8,
        num: 1,
        checked: true,
      }
      
    ],
    showPopup : false,
    name:'',
    phone:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.updateTotal();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    const goods = wx.getStorageSync("goods");
    console.log(goods);
    this.setData({
      goods:goods,
      name:getApp().globalData.userInfo.nickName,
      phone:getApp().globalData.userInfo.phonenumber
    })
    this.updateTotal();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  // 单个商品勾选状态变化
  onItemCheckChange(e) {
    const index = e.currentTarget.dataset.index;
    const goods = this.data.goods;
    goods[index].checked = !goods[index].checked;
    this.updateTotal();
    if(this.data.totalCount==this.data.goods.length){
      this.setData({
        allChecked:true
      })
    }else{
      this.setData({
        allChecked:false
      })
    }
    this.setData({
      goods
    });
  },

  // 全选按钮变化
  onAllCheckChange(e) {
    const allChecked = e.detail;
    const goods = this.data.goods.map(item => ({
      ...item,
      checked: allChecked
    }));
    this.setData({
      goods,
      allChecked
    });
    this.updateTotal();
  },
    // 步进器变化
  onStepperChange(e) {
    const index = e.currentTarget.dataset.index;
    const goods = this.data.goods;
    goods[index].nums = e.detail;
    request({
      url:"/dianshang/cart",
      method:"PUT",
      header: {
        "authorization": getApp().globalData.authorization
      },
      data:{
        id:goods[index].id,
        nums:goods[index].nums
      }
    })
    this.updateTotal();
    this.setData({
      goods
    });
  },

  // 更新总价和总数量
  updateTotal() {
    const goods = this.data.goods;
    let totalPrice = 0.0;
    let totalCount = 0;

    goods.forEach(item => {
      if (item.checked) {
        totalPrice += item.aGoods.money * item.nums;
        totalCount += 1;
      }
    });

    this.setData({
      totalPrice: totalPrice.toFixed(2), // 保留两位小数
      totalCount
    });
  },

  onSubmit() {
    this.setData({
      showPopup: true
    })
  },
  onCancle() {
    this.setData({
      showPopup: false
    })
  },
  onQueRen(){
    const goods = this.data.goods;
    for(let i=0;i<goods.length;i++){
      if(goods[i].checked){
        // dingdan.push()
        request({
          url:"/dianshang/orders",
          method:"POST",
          header: {
            "authorization": getApp().globalData.authorization
          },
          data:{
            userid:getApp().globalData.userInfo.userId,
            goodsid:goods[i].aGoods.id,
            nums:goods[i].nums,
            creat:new Date(),
            status:1,
            del:0
          }
        }).then(data=>{
          if(i==goods.length-1&&!goods[i].checked){
            wx.setStorageSync("dingdan",this.data.dingdan);
            wx.navigateTo({
              url: '/pages/go_wu_che_gopay/go_wu_che_gopay?money='+this.data.totalPrice,
            })
          }
          if(data.code==200){
            this.data.dingdan.push(data.data.id);
            if(i==goods.length-1){
              wx.setStorageSync("dingdan",this.data.dingdan);
              wx.navigateTo({
                url: '/pages/go_wu_che_gopay/go_wu_che_gopay?money='+this.data.totalPrice,
              })
            }
            request({
              url:"/dianshang/cart/"+goods[i].id+"/",
              header: {
                "authorization": getApp().globalData.authorization
              },
              method:"DELETE"
            }).then(data=>{
              
            })
          }
        })
      }
    }
   
  },
  back(){
    wx.navigateBack()
  }
})